var SearchResults = {
	movieList:null,
	pageSize:null,
	totalCount:null,
	indexes:null,
	startIndex:null,
	
	searchWord:null,
	searchScope:null,
	isAsc:null,
	formFlag:false,
	classifier:null,
	orderBy:null,
	
	actionType:null,
	query:null,
	
	createQuery : function() {
		this.query = "&searchWord="+this.searchWord+"&searchScope="+this.searchScope+"&orderBy="+this.orderBy+"&isAsc="+this.isAsc;
		if(this.actionType == "movieSearch") {
			this.query = this.query + "&formFlag="+this.formFlag;
		}
	},
	
	// load search result and display
	loadMovieResults : function (ajax_request) {
		var _nextIndex = eval(this.startIndex + "+" + this.pageSize);
		var _previousIndex = eval(this.startIndex + "-" + this.pageSize);
		
		// display number & total count 2/2
		var _display_number = document.getElementById("display_number"); // page_size
		var _result_number = document.getElementById("result_number"); // total_number
		
		var _startIndex = eval(this.startIndex + " + 1");
		
		if(this.totalCount == 0) {
			_display_number.innerHTML = 0;
		}
		else if(len < this.pageSize) {
			_display_number.innerHTML = _startIndex + "-" + len;
		}
		else {
			_display_number.innerHTML = _startIndex + "-" +_nextIndex;
		}
		_result_number.innerHTML = this.totalCount;
		
		// display footer
		var _previousIndexEle = document.getElementById("previous_index");
		var _indexes = document.getElementById("indexes");
		var _nextIndexEle = document.getElementById("next_index");
		
		this.createQuery();
		
		if(_nextIndex < this.totalCount) {
			_nextIndexEle.innerHTML = "<a href='"+this.actionType+".do?startIndex="+_nextIndex+this.query+"'>下一页</a>";	
		}
		if (this.startIndex > 0) {
			_previousIndexEle.innerHTML = "<a href='"+this.actionType+".do?startIndex="+_previousIndex+this.query+"'>上一页</a>";
		}
		
		var _html = "";
		var indexArray = eval(this.indexes);
		for(var n=1;n<=indexArray.length;n++) {
			if(this.startIndex == indexArray[n-1])
				_html = _html + "<span>" + n + "</span>";
			else
				_html = _html + "<a href='"+this.actionType+".do?startIndex="+indexArray[n-1]+this.query+"'>" + n + "</a>";
				
			_html = _html + "<span> </span>";
		}
		_indexes.innerHTML = _html;

		
		// process data
		var data;
		if(ajax_request != null) {
			data = eval("(" + ajax_request.responseText + ")");
		}
		else 
			data = dojo.fromJson(this.movieList);
			
		var len = data.movies.length;
		
		for(var i=0;i<data.movies.length;i++) {
			var _movie_item = document.getElementById("res_"+i+"_movie_item"); // change style
			var _dotted_line = document.getElementById("res_"+i+"_dotted_line");
			var _img = document.getElementById("res_"+i+"_img");// set movie image
			var hrefArray = document.getElementsByName("res_"+i+"_href"); // set movieInfo href
			var _name = document.getElementById("res_"+i+"_name");// set movie name
			var _item_sort_num = document.getElementById("item_"+i+"_sort_num"); // set item sort number
			var _director_name = document.getElementById("res_"+i+"_director_names"); // set director name
			var _actor_name = document.getElementById("res_"+i+"_actors"); // set actor names
			var _type = document.getElementById("res_"+i+"_type"); // set movie type
			
			_movie_item.style.display = "";
			_dotted_line.style.display = "";
			_img.src = window.basePath+data.movies[i].moviePoster;
			_name.innerHTML = data.movies[i].movieName;
			_item_sort_num.innerHTML = eval(this.startIndex + " + 1 +" + i)+". ";
			_director_name.innerHTML = data.movies[i].movieDirector;
			_type.innerHTML = data.movies[i].movieType;
			
			var actorNamesData = data.movies[i].actorNames;
			var _actor_name_array = actorNamesData.split(",");
			if(_actor_name_array.length>1) {
				var _actor_name_display = "";
				
				if(_actor_name_array.length<3) {
					for(var k=0;k<_actor_name_array.length;k++) {
						_actor_name_display = _actor_name_display + _actor_name_array[k] + " "; 
					}
				}
				else {
					for(var k=0;k<3;k++) {
						_actor_name_display = _actor_name_display + _actor_name_array[k] + " "; 
					}
					if (_actor_name_display>3)
						_actor_name_display = _actor_name_display + "等" 
				}
				
				_actor_name.innerHTML = _actor_name_display;
			}
			else // only have a actor
				_actor_name.innerHTML = _actor_name_array[0];
			
			var movieID = data.movies[i].movieID;
			for(var j=0;j<hrefArray.length;j++) {
				hrefArray[j].href = "movieInfo.jsp?movieID="+movieID;
			}
			
		}
	},
	
	changeOrder : function () {
		var _sort_by = document.getElementById("sortBy");
		var _sort_by_value = _sort_by.value;
		var _index_of_ = _sort_by_value.indexOf('_');
		
		this.orderBy = _sort_by_value.substring(0,_index_of_);
		this.isAsc = (_sort_by_value.substring(_index_of_+1) == "asc");
	//	this.query = "&searchWord="+this.searchWord+"&searchScope="+this.searchScope+"&orderBy="+this.orderBy+"&isAsc="+this.isAsc+"&formFlag="+this.formFlag;
		this.createQuery();
		
		window.location.href = window.basePath + this.actionType+".do?startIndex=0"+this.query;
	//	this.simpleSearchAction(this.query);
	},
	
	changeYear : function () {
		var year = dijit.byId("yearOfPublish").attr("id");
		alert(year);
	}
	

};

function checkSimpleSearch() {
	var searchWordEle = document.getElementById("searchWord");
	if(searchWordEle.value == "" ) {
		return false;
	}
	else 
		return true;
}

function checkAdvancedSearch() {
	var categoryName = document.getElementById("categoryName");
	var publishTime = document.getElementById("publishTime");
	
	if(categoryName.value == "") {
		alert("请选择类别");
		return false;
	}
	if(publishTime.value == "") {
		alert("请选择年份");
		return false;
	}
	
	return true;
}
